<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="header-info">
						<text class="tool-icon">💼</text>
						<text class="tool-title">工作占卜</text>
						<text class="tool-subtitle">工作运势和职场分析</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">基本信息</text>
						
						<view class="form-item">
							<text class="form-label">当前职业</text>
							<picker 
								mode="selector" 
								:range="careerOptions" 
								:value="careerIndex"
								@change="onCareerChange"
								class="career-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayCareer }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">工作年限</text>
							<picker 
								mode="selector" 
								:range="experienceOptions" 
								:value="experienceIndex"
								@change="onExperienceChange"
								class="experience-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayExperience }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">关注方面</text>
							<view class="focus-selector">
								<view 
									v-for="focus in focusAreas" 
									:key="focus.value"
									class="focus-item"
									:class="{ active: params.focus === focus.value }"
									@tap="selectFocus(focus.value)"
								>
									<text class="focus-icon">{{ focus.icon }}</text>
									<text class="focus-text">{{ focus.name }}</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">具体问题（可选）</text>
							<textarea 
								v-model="params.question" 
								placeholder="请描述您关心的具体事业问题"
								class="question-input"
								maxlength="200"
							></textarea>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '占卜中...' : '开始占卜'" 
						type="primary"
						@tap="queryCareer"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="careerResult">
					<view class="result-section">
						<text class="section-title">事业分析</text>
						
						<view class="career-analysis">
							<view class="overall-career">
								<view class="career-score">
									<view class="score-circle">
										<text class="score-text">{{ careerResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ careerResult.overall.level }}</text>
								</view>
								<view class="career-desc">
									<text class="desc-text">{{ careerResult.overall.description }}</text>
								</view>
							</view>
							
							<view class="career-details">
								<view 
									v-for="(detail, index) in careerResult.details" 
									:key="index"
									class="detail-item"
								>
									<view class="detail-header">
										<text class="detail-aspect">{{ detail.aspect }}</text>
										<view class="detail-score">
											<text class="score-text">{{ detail.score }}分</text>
										</view>
									</view>
									<text class="detail-description">{{ detail.description }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 运势趋势 -->
				<app-card v-if="careerResult && careerResult.trends">
					<view class="trends-section">
						<text class="section-title">运势趋势</text>
						
						<view class="trends-timeline">
							<view 
								v-for="(trend, index) in careerResult.trends" 
								:key="index"
								class="trend-item"
							>
								<view class="trend-time">
									<text class="time-text">{{ trend.period }}</text>
								</view>
								<view class="trend-content">
									<view class="trend-level" :class="trend.level.toLowerCase()">
										<text class="level-text">{{ trend.level }}</text>
									</view>
									<text class="trend-desc">{{ trend.description }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 建议 -->
				<app-card v-if="careerResult && careerResult.suggestions">
					<view class="suggestions-section">
						<text class="section-title">事业建议</text>
						
						<view class="suggestions-list">
							<view 
								v-for="(suggestion, index) in careerResult.suggestions" 
								:key="index"
								class="suggestion-item"
							>
								<view class="suggestion-icon">
									<text class="icon-text">💡</text>
								</view>
								<text class="suggestion-text">{{ suggestion }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 幸运元素 -->
				<app-card v-if="careerResult && careerResult.luckyElements">
					<view class="lucky-section">
						<text class="section-title">事业幸运元素</text>
						
						<view class="lucky-elements">
							<view class="element-item">
								<text class="element-label">幸运色</text>
								<text class="element-value">{{ careerResult.luckyElements.color }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">幸运数字</text>
								<text class="element-value">{{ careerResult.luckyElements.number }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">吉利方位</text>
								<text class="element-value">{{ careerResult.luckyElements.direction }}</text>
							</view>
							<view class="element-item">
								<text class="element-label">吉时</text>
								<text class="element-value">{{ careerResult.luckyElements.time }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'

export default {
	name: 'CareerToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				career: '',
				experience: '',
				focus: '',
				question: ''
			},
			careerResult: null,
			loading: false,
			toolsService: null,
			careerIndex: 0,
			experienceIndex: 0,
			careerOptions: [
				'IT/互联网', '金融/投资', '教育/培训', '医疗/健康', 
				'制造业', '服务业', '政府机关', '自主创业', 
				'销售/市场', '设计/创意', '学生', '其他'
			],
			experienceOptions: [
				'应届毕业生', '1-3年', '3-5年', '5-10年', '10年以上'
			],
			focusAreas: [
				{ value: 'promotion', name: '升职加薪', icon: '📈' },
				{ value: 'change', name: '跳槽转行', icon: '🔄' },
				{ value: 'startup', name: '创业机会', icon: '🚀' },
				{ value: 'relationship', name: '人际关系', icon: '🤝' },
				{ value: 'development', name: '职业发展', icon: '📊' },
				{ value: 'stability', name: '工作稳定', icon: '🛡️' }
			]
		}
	},
	computed: {
		canQuery() {
			return this.params.career && this.params.experience && this.params.focus
		},
		
		displayCareer() {
			return this.params.career || '选择职业'
		},
		
		displayExperience() {
			return this.params.experience || '选择工作年限'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
		},
		
		onCareerChange(e) {
			this.careerIndex = e.detail.value
			this.params.career = this.careerOptions[e.detail.value]
		},
		
		onExperienceChange(e) {
			this.experienceIndex = e.detail.value
			this.params.experience = this.experienceOptions[e.detail.value]
		},
		
		selectFocus(focus) {
			this.params.focus = focus
		},
		
		async queryCareer() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请完善基本信息',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getCareerInfo(this.params)
				
				if (result.success) {
					this.careerResult = result.data
					uni.showToast({
						title: '占卜完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '占卜失败')
				}
			} catch (error) {
				console.error('事业占卜失败:', error)
				uni.showToast({
					title: '占卜失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.career-picker,
		.experience-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
		
		.focus-selector {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 12rpx;
			
			.focus-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 16rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #50E3C2;
					background: linear-gradient(135deg, #50E3C2 0%, #26A69A 100%);
					
					.focus-icon,
					.focus-text {
						color: white;
					}
				}
				
				.focus-icon {
					font-size: 28rpx;
					margin-bottom: 8rpx;
				}
				
				.focus-text {
					font-size: 22rpx;
					color: #000000;
				}
			}
		}
		
		.question-input {
			width: 100%;
			min-height: 120rpx;
			padding: 16rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
			line-height: 1.5;
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.career-analysis {
		.overall-career {
			display: flex;
			align-items: center;
			margin-bottom: 32rpx;
			padding: 32rpx;
			background: linear-gradient(135deg, #50E3C2 0%, #26A69A 100%);
			border-radius: 16rpx;
			
			.career-score {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 32rpx;
				
				.score-circle {
					width: 120rpx;
					height: 120rpx;
					border: 4rpx solid rgba(255, 255, 255, 0.3);
					border-radius: 50%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 12rpx;
					
					.score-text {
						font-size: 36rpx;
						font-weight: bold;
						color: white;
					}
					
					.score-unit {
						font-size: 20rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}
				
				.score-level {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.career-desc {
				flex: 1;
				
				.desc-text {
					font-size: 28rpx;
					color: white;
					line-height: 1.6;
				}
			}
		}
		
		.career-details {
			display: flex;
			flex-direction: column;
			gap: 16rpx;
			
			.detail-item {
				padding: 20rpx;
				background-color: #f8f9fa;
				border-radius: 12rpx;
				
				.detail-header {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 12rpx;
					
					.detail-aspect {
						font-size: 28rpx;
						font-weight: 500;
						color: #000000;
					}
					
					.detail-score {
						.score-text {
							font-size: 24rpx;
							color: #50E3C2;
							font-weight: 500;
						}
					}
				}
				
				.detail-description {
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.8);
					line-height: 1.5;
				}
			}
		}
	}
}

.trends-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.trends-timeline {
		.trend-item {
			display: flex;
			margin-bottom: 24rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.trend-time {
				width: 120rpx;
				margin-right: 20rpx;
				
				.time-text {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.7);
				}
			}
			
			.trend-content {
				flex: 1;
				padding: 16rpx;
				background-color: #f8f9fa;
				border-radius: 12rpx;
				
				.trend-level {
					display: inline-block;
					padding: 4rpx 12rpx;
					border-radius: 12rpx;
					margin-bottom: 8rpx;
					
					&.good {
						background-color: #4CAF50;
					}
					
					&.normal {
						background-color: #FF9800;
					}
					
					&.poor {
						background-color: #F44336;
					}
					
					.level-text {
						font-size: 20rpx;
						color: white;
						font-weight: 500;
					}
				}
				
				.trend-desc {
					display: block;
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.8);
					line-height: 1.5;
				}
			}
		}
	}
}

.suggestions-section,
.lucky-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.suggestions-list {
		.suggestion-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.suggestion-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
				
				.icon-text {
					font-size: 32rpx;
				}
			}
			
			.suggestion-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
	
	.lucky-elements {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 16rpx;
		
		.element-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 24rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			.element-label {
				font-size: 24rpx;
				color: rgba(0, 0, 0, 0.7);
				margin-bottom: 8rpx;
			}
			
			.element-value {
				font-size: 28rpx;
				font-weight: 500;
				color: #50E3C2;
			}
		}
	}
}
</style>